var wxCharts = require('../../utils/wxcharts.js');
var config = require('../../url.js');
//var app = getApp();
var mmpCanvas = null;
var interestCanvas = null;
var totalCanvas = null;
Page({
  data: {
    version: '',   //檢測版本
    currentTab: 0, //當前頁
    windowHeight: '',
    today: '',
    array: ['請選擇', '2.00', '2.15', '2.35', '2.75', '3', '3.5', '3.75', '4', '4.41', '4.5', '5', '5.15', '5.5'],
    index: '0',
    array2: ['2.00', '2.15', '2.35', '2.75', '3', '3.5', '3.75', '4', '4.41', '4.5', '5', '5.15', '5.5'],
    index2: '1',
    setInterestFixed: 2.375, // 設置銀行最優惠按揭利率 超過算2.375
    feedback: "1%-1.95%",  // 轉按客戶現金回贈

    np: 60,        // 45503 以上为60 以下为50
    pressure_rate: 0, //压力测试

    prime_rate: 0,  //prime_rate
    suoxi_rate: 0,  //琐息

    monthInfo: 0,  //月利率
    yearInfo: 0,   //年利率
    fixed: 0,      //固定利率
    interestFixed: 0,  //最新按揭利率 月+固定利率

    repay: 30,  //還款期（年）
    percent: 60,  //按揭成數（60%）
    yearInterest: 215, //調整的利率（2.15%）

    house: 0, //樓價
    down: 0,  //首期
    loan: 0,  //貸款總額
    interest: 0,  //利息
    iconI: 2,  //利息升降提示+
    minIncome: 0, //最低月收入

    tips: '',     //計算失敗提示
    premium: 0,   //保費
    interestMonthPay: 0, //月供
    allPay: 0,     //總供款

    cInterest: 0, //調整利率利息
    cInterestMonthPay: 0, //調整利率後月供

    hk_has_null: 0, //物業買賣印花稅
    hk_has_more: 0, //從價印花稅
    not_hk: 0,      //買家印花稅

    extra_6: 0,     //額外印花稅6個月以內
    extra_6_12: 0,  //額外印花稅6-12個月
    extra_12_36: 0, //額外印花稅12-36個月


    tRepay: 30,   //借貸能力-還款期
    income: 0,    //借貸能力-我的收入
    showIncome: 0,
    tLoan: 0,     //借貸能力-貸款額
    tInterest: 0, //借貸能力-總利息
    total: 0,   //借貸能力-總還款，本金+利息
    totalMonthPay: 0, //借貸能力-月供

    changeLoanAfter: 0,     //轉按後貸款
    changeLoanBefore: 0,    //轉按前貸款
    changeMonthPay: 0,  // 月供變化
    beforePay: 0,    // 轉按前月供
    afterPay: 0,     // 轉按後月供
    showBefore: 0,
    showAfter: 0,
    changeRepay: 30, //轉按還款期
    changeRepay2: 30,
    oldInterest: 2.15,  // 舊按揭利率
    oldPercent: 60,  // 按揭成數
    tips2: '',
    oldc: 0,     // 舊還款利息
    newc: 0,     // 新還款利息
    changec: 0, // 總利息變化

    //還款詳細表
    repayment: [],

    showHibor: false, //查看今日最新按揭利率
    allSx: false, //查看所有稅項
    repayList: false, //查看還款詳細表
    howMoney: false //多少人工可以上車
  },
  onShareAppMessage: function () {
    var that = this;
    console.log(that.data.house)
    return {
      title: '月供:' + that.data.allPay,
      path: '/pages/index/share?house='+that.data.house+'&repay='+that.data.repay+'&percent='+that.data.percent
    }
  },
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
    this.changeInterest(this.data.array[e.detail.value]);
  },
  bindPickerChange2: function (e) {
    this.setData({
      oldInterest: this.data.array2[e.detail.value],
      index2: e.detail.value
    })
    if (this.data.changeLoanBefore > 0 || this.data.changeLoanBefore != '') {
      this.reckon3(this.data.changeLoanBefore);
    }
  },
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  },
  changeYear(e) {
    this.setData({ repay: e.detail.value });
    if (this.data.house > 0 || this.data.house != '') {
      this.reckon(this.data.house);
    }
  },
  changePercent(e) {
    this.setData({ percent: e.detail.value });
    if (this.data.house > 0 || this.data.house != '') {
      this.reckon(this.data.house);
    }
  },
  changeInterest(e) {
    let that = this;
    let rate = e;
    let nper = that.data.repay * 12;
    let housePrice = this.data.house;
    wx.request({
      url: config.http + '/api/index/payment-info',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        price: housePrice * 10000,
        rate: rate,
        nper: nper,
        mortgage_rate: that.data.percent
      },
      method: 'POST',
      success: function (res) {
        //console.log(res.data)
        let repays = res.data.data.total;

        that.setData({
          cInterest: parseFloat((repays[2] / 10000).toFixed(1)),
          cInterestMonthPay: that.formatMoney(parseFloat((repays[4] * 1).toFixed(2)), 2)
        })
      },
      complete: function () {
        that.current2();
        if (that.data.cInterest > that.data.interest) {
          that.setData({ iconI: 1 })
        } else if (that.data.cInterest < that.data.interest) {
          that.setData({ iconI: 0 })
        }
      },
      fail: function (res) {
        // fail
      },
    })
  },
  changeTRepay(e) {
    this.setData({ tRepay: e.detail.value });
    if (this.data.income > 0 || this.data.income != '') {
      this.reckon2(this.data.income);
    }
  },
  refresh_repay_rekon3(e) {
    this.setData({ changeRepay: e.detail.value });
    if (this.data.changeLoanBefore > 0 || this.data.changeLoanBefore != '') {
      this.reckon3(this.data.changeLoanBefore);
    }
  },
  newRepay(e) {
    this.setData({ changeRepay2: e.detail.value });
    if (this.data.changeLoanAfter > 0 || this.data.changeLoanAfter != '') {
      this.reckon4(this.data.changeLoanAfter);
    }
  },
  formatMoney(s1, n) {     //  n为小数点位数
    var s = parseFloat((s1 + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";     //每一位都用 空格隔开
    var l = s.split(".")[0].split("").reverse();
    var r = "";
    var gg = null;
    if (s.indexOf(".") > 0) {   //判断是否包含小数点
      r = s.split(".")[1];
    }

    var t = "";
    for (var i = 0; i < l.length; i++) {
      t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
    }
    if (s.indexOf(".") > 0) {
      gg = t.split("").reverse().join("") + "." + r;
    } else {
      gg = t.split("").reverse().join("");
    }

    return gg;

  },
  reckon(e) {
    let housePrice;
    if (Number(e)) {
      housePrice = e;
    } else {
      housePrice = e.detail.value;
    }
    this.setData({ house: housePrice });

    let down = housePrice - (housePrice * (this.data.percent / 100));
    let loan = housePrice * (this.data.percent / 100);

    let that = this;
    let rate = that.data.monthInfo + that.data.fixed;
    if (rate >= that.data.setInterestFixed){
      rate = that.data.setInterestFixed
    }
    let pressure_rate = that.data.pressure_rate;
    let nper = that.data.repay * 12;

    if (housePrice == '' || this.data.percent > 90) {
      this.setData({
        down: 0,
        loan: 0,
        allPay: 0,
        minIncome: 0,
        interest: 0,
        cInterest: 0,
        cInterestMonthPay: 0,
        premium: 0
      });
    } else {
      wx.request({
        url: config.http + '/api/index/payment-info',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        data: {
          price: housePrice * 10000,
          rate: pressure_rate,
          nper: nper,
          mortgage_rate: that.data.percent
        },
        method: 'POST',
        success: function (res) {
          console.log(res)
          if (res.data.status == 0) {
            that.setData({ minIncome: 0 })
          } else {
            if (that.data.percent <= 80){
              that.setData({
                minIncome: that.formatMoney(parseFloat(((res.data.data.total[4] * 1) / 0.6).toFixed(2)), 2)
                //最低月入要求
              })
            }else{
              that.setData({
                minIncome: that.formatMoney(parseFloat(((res.data.data.total[4] * 1) / 0.55).toFixed(2)), 2)
                //最低月入要求
              })
            }
          }
        }
      })

      wx.request({
        url: config.http + '/api/index/payment-info',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        data: {
          price: housePrice * 10000,
          rate: rate,
          nper: nper,
          mortgage_rate: that.data.percent
        },
        method: 'POST',
        success: function (res) {
          console.log(res.data)

          if (res.data.status == 0) {
            that.setData({
              down: 0,
              loan: 0,
              allPay: 0,
              //minIncome:0,
              interest: 0,
              cInterest: 0,
              cInterestMonthPay: 0,
              premium: 0,
              tips: res.data.info
            });
          } else {
            let repays = res.data.data.total; //還款列表
            let stamp_duty = res.data.data.stamp_duty; //稅務列表

            that.setData({
              tips: '',
              house: housePrice,      //房价
              down: parseFloat(down.toFixed(1)),  //首期
              loan: parseFloat(loan.toFixed(1)),  //贷款

              interest: parseFloat((repays[2] / 10000).toFixed(1)),  //總利息
              cInterest: parseFloat((repays[2] / 10000).toFixed(1)),
              hk_has_null: stamp_duty.hk_has_null / 10000, //物業買賣印花稅
              hk_has_more: stamp_duty.hk_has_more / 10000, //從價印花稅
              not_hk: stamp_duty.not_hk / 10000,      //買家印花稅

              extra_6: stamp_duty.extra[0] / 10000,     //額外印花稅6個月以內
              extra_6_12: stamp_duty.extra[1] / 10000,  //額外印花稅6-12個月
              extra_12_36: stamp_duty.extra[2] / 10000, //額外印花稅12-36個月

              //minIncome: that.formatMoney(parseFloat((res.data.data.min_income * 1).toFixed(2)), 2),  //最低月入要求

              premium: parseFloat((repays[6] * 1).toFixed(2)),            //保費
              interestMonthPay: that.formatMoney(parseFloat((repays[5] * 1).toFixed(2)), 2),   //月供
              cInterestMonthPay: that.formatMoney(parseFloat((repays[4] * 1).toFixed(2)), 2),
              allPay: that.formatMoney(parseFloat((repays[4] * 1).toFixed(2)), 2),             //總還款

            })
            //{num:1,principal:3563,repayInterest:3225,monthPay:6789}
            var arr = [];
            for (var i = 1; i < res.data.data.result.length; i++) {
              var result = res.data.data.result[i];
              arr.push({ num: result[0], principal: that.formatMoney(result[3], 2), repayInterest: that.formatMoney(result[2], 2), monthPay: that.formatMoney(result[1], 2) });
            }
            that.setData({
              repayment: arr
            })

          }
        },
        complete: function () {
          that.current0();
          if (that.data.index > 0) {
            that.changeInterest(that.data.array[that.data.index]);
          }
        },
        fail: function (res) {
          // fail
        },
      })


    }

  },
  reckon2(e) {
    let income;
    if (Number(e)) {
      income = e;
    } else {
      income = e.detail.value;
    }
    this.setData({ income: income, showIncome: this.formatMoney(income, 0) });


    let that = this;
    let rate = that.data.monthInfo + that.data.fixed;
    if (rate >= that.data.setInterestFixed) {
      rate = that.data.setInterestFixed
    }
    let rateS = that.data.prime_rate - that.data.suoxi_rate;
    let nper = that.data.tRepay * 12;
    let pressure_rate = that.data.pressure_rate;

    wx.request({
      url: config.http + '/api/index/loan-by-payment',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        income: income,
        rate: rate,
        nper: nper,
        pressure_rate: pressure_rate
      },
      method: 'POST',
      success: function (res) {
        //console.log(res.data)
        var repay = res.data.data.total;
        var tLoan = parseFloat((res.data.data.pv / 10000).toFixed(1));
        if (income <= 45503) {
          that.setData({
            //income: income,    //借貸能力-我的收入
            np: 60,
            tLoan: tLoan,     //借貸能力-最高貸款額
            tInterest: parseFloat((tLoan / 0.6 - tLoan).toFixed(1)), //借貸能力-首期
            total: parseFloat((tLoan / 0.6).toFixed(1)),   //借貸能力-可负担楼价
            totalMonthPay: parseFloat((res.data.data.pmt).toFixed(2))
          })
        } else {
          that.setData({
            //income: income,    //借貸能力-我的收入
            np: 50,
            tLoan: tLoan,     //借貸能力-最高貸款額
            tInterest: parseFloat((tLoan / 0.5 - tLoan).toFixed(1)), //借貸能力-首期
            total: parseFloat((tLoan / 0.5).toFixed(1)),   //借貸能力-可负担楼价
            totalMonthPay: parseFloat((res.data.data.pmt).toFixed(2))
          })
        }
      },
      complete: function(){
        that.current1();
      }
    })

  },
  reckon3: function (e) {
    let that = this;

    let loan, changeLoanAfter;

    if (Number(e)) {
      loan = e;
    } else {
      loan = e.detail.value;
    }

    that.setData({
      changeLoanBefore: loan
    });

    if (that.data.changeLoanAfter != ''){
      changeLoanAfter = that.data.changeLoanAfter;
    }else{
      changeLoanAfter = loan;
    }

    //275.3 - 2606.2

    let nper = that.data.changeRepay * 12;
    let rate = that.data.array2[that.data.index2];

    let newRate = that.data.monthInfo + that.data.fixed;
    if(newRate >= that.data.setInterestFixed){
      newRate = that.data.setInterestFixed
    }
    let nper2 = that.data.changeRepay2 * 12;
    wx.request({
      url: config.http + '/api/index/exchange-payment',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        rate: rate,
        pv:loan * 10000,
        nper: nper
      },
      method: 'POST',
      success: function (res) {

        that.setData({
          beforePay: parseFloat((res.data.data.pmt * 1).toFixed(2)),
          showBefore: that.formatMoney(parseFloat((res.data.data.pmt * 1).toFixed(2)), 2)
        })
      },
      complete: function () {
        wx.request({
          url: config.http + '/api/index/exchange-payment',
          header: { 'content-type': 'application/x-www-form-urlencoded' },
          data: {
            rate: newRate,
            pv: changeLoanAfter * 10000,
            nper: nper2
          },
          method: 'POST',
          success: function (res) {
            //console.log(res)
            // var monthPay = res.data.data.total[4];
            // var newc = res.data.data.total[2];
            that.setData({
              afterPay: parseFloat((res.data.data.pmt * 1).toFixed(2)),
              showAfter: that.formatMoney(parseFloat((res.data.data.pmt * 1).toFixed(2)), 2)
            })
          },
          complete: function () {
            var changeMonthPay = parseFloat(that.data.beforePay) - parseFloat((that.data.afterPay));
            if (changeMonthPay > 0) {
              changeMonthPay = '-' + that.formatMoney(changeMonthPay.toFixed(2), 2);
            } else {
              changeMonthPay = "+" + that.formatMoney(Math.abs(changeMonthPay).toFixed(2), 2);
            }
            var changec = (((parseFloat(that.data.beforePay) * nper) - (loan * 10000)) - ((parseFloat(that.data.afterPay) * nper2) - (changeLoanAfter*10000))) / 10000;

            if (changec > 0) {
              changec = '-' + changec.toFixed(2);
            } else {
              changec = "+" + Math.abs(changec).toFixed(2);
            }
            that.setData({
              changeMonthPay: changeMonthPay,
              changec: changec
            })
          }
        })
      }
    })



  },

  reckon4: function (e) {
    let that = this;

    let loan, changeLoanBefore;
    if (Number(e)) {
      loan = e;
    } else {
      loan = e.detail.value;
    }
    that.setData({
      changeLoanAfter: loan
    });

    if (that.data.changeLoanBefore!=''){
      changeLoanBefore = that.data.changeLoanBefore;
    }else{
      changeLoanBefore = loan;
    }

    let nper = that.data.changeRepay * 12;
    let rate = that.data.array2[that.data.index2];

    let newRate = that.data.monthInfo + that.data.fixed;
    if (newRate >= that.data.setInterestFixed) {
      newRate = that.data.setInterestFixed
    }
    let nper2 = that.data.changeRepay2 * 12;
    wx.request({
      url: config.http + '/api/index/exchange-payment',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        rate: rate,
        pv: changeLoanBefore * 10000,
        nper: nper
      },
      method: 'POST',
      success: function (res) {

        that.setData({
          beforePay: parseFloat((res.data.data.pmt * 1).toFixed(2)),
          showBefore: that.formatMoney(parseFloat((res.data.data.pmt * 1).toFixed(2)), 2)
        })
      },
      complete: function () {
        wx.request({
          url: config.http + '/api/index/exchange-payment',
          header: { 'content-type': 'application/x-www-form-urlencoded' },
          data: {
            rate: newRate,
            pv: loan * 10000,
            nper: nper2
          },
          method: 'POST',
          success: function (res) {
            that.setData({
              afterPay: parseFloat((res.data.data.pmt * 1).toFixed(2)),
              showAfter: that.formatMoney(parseFloat((res.data.data.pmt * 1).toFixed(2)), 2)
            })
          },
          complete: function () {
            var changeMonthPay = parseFloat(that.data.beforePay) - parseFloat((that.data.afterPay));
            if (changeMonthPay > 0) {
              changeMonthPay = '-' + that.formatMoney(changeMonthPay.toFixed(2), 2);
            } else {
              changeMonthPay = "+" + that.formatMoney(Math.abs(changeMonthPay).toFixed(2), 2);
            }
            var changec = (((parseFloat(that.data.beforePay) * nper) - (changeLoanBefore*10000)) - ((parseFloat(that.data.afterPay) * nper2)-(loan*10000))) / 10000;
            if (changec > 0) {
              changec = '-' + changec.toFixed(2);
            } else {
              changec = "+" + Math.abs(changec).toFixed(2);
            }
            that.setData({
              changeMonthPay: changeMonthPay,
              changec: changec
            })
          }
        })
      }
    })


  },
  /** 
   * 点击tab切换 
   */
  swichNav: function (e) {

    var that = this;

    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }

    if (this.data.currentTab == 0) {
      this.current0();
    } else if (this.data.currentTab == 1) {
      this.current1();
    }
  },

  showSxList: function () {
    if (this.data.allSx == false) {
      this.setData({
        allSx: true
      })
    } else {
      this.setData({
        allSx: false
      })
    }
  },
  showHibor: function () {
    if (this.data.showHibor == false) {
      this.setData({
        showHibor: true
      })
    } else {
      this.setData({
        showHibor: false
      })
    }
  },
  showRepaymentList: function () {
    if (this.data.repayList == false) {
      this.setData({
        repayList: true
      })
    } else {
      this.setData({
        repayList: false
      })
    }
  },
  showMoneyList: function () {
    if (this.data.howMoney == false) {
      this.setData({
        howMoney: true
      })
    } else {
      this.setData({
        howMoney: false
      })
    }
  },
  current0: function (price) {
    if (wx.openBluetoothAdapter) {
      wx.openBluetoothAdapter();
      var imp = wx.createContext();
      this.textCharts(imp, 'interest-month-pay', this.data.allPay);
      var cImp = wx.createContext();
      this.textCharts(cImp, 'cInterest-month-pay', this.data.cInterestMonthPay);
    } else {
      this.setData({
        version: 'old'
      })
    }
    if (this.data.house > 0 || this.data.house != '') {
      mmpCanvas.updateData({
        series: [{
          name: 'down',
          data: JSON.parse(this.data.down),
          color: '#ff979b',
        }, {
          name: 'loan',
          data: JSON.parse(this.data.loan),
          color: '#f9545b'
        }, {
          name: 'interest',
          data: JSON.parse(this.data.interest),
          color: '#bb151c'
        }]
      });

      interestCanvas.updateData({
        series: [{
          name: 'down',
          data: JSON.parse(this.data.down),
          color: '#ff979b',
        }, {
          name: 'loan',
          data: JSON.parse(this.data.loan),
          color: '#f9545b'
        }, {
          name: 'interest',
          data: JSON.parse(this.data.cInterest),
          color: '#bb151c'
        }]
      });
    } else {
      mmpCanvas.updateData({
        series: [{
          name: 'down',
          data: 1,
          color: '#ff979b',
        }, {
          name: 'loan',
          data: 1,
          color: '#f9545b'
        }, {
          name: 'interest',
          data: 1,
          color: '#bb151c'
        }]
      });

      interestCanvas.updateData({
        series: [{
          name: 'down',
          data: 1,
          color: '#ff979b',
        }, {
          name: 'loan',
          data: 1,
          color: '#f9545b'
        }, {
          name: 'interest',
          data: 1,
          color: '#bb151c'
        }]
      });
    }
  },
  current1: function () {
    if (wx.openBluetoothAdapter) {
      wx.openBluetoothAdapter();
      var total = wx.createContext();
      this.textCharts(total, 'total-month-repayment', this.data.totalMonthPay);
    } else {
      this.setData({
        version: 'old'
      })
    }

    if (this.data.income > 0 || this.data.income != '') {
      totalCanvas.updateData({
        series: [{
          name: 'monthPay',
          data: JSON.parse((this.data.income) / 10000),
          color: '#ff979b'
        }, {
          name: 'tLoan',
          data: JSON.parse(this.data.tLoan),
          color: '#f9545b',
        }, {
          name: 'loan',
          data: JSON.parse(this.data.total),
          color: '#bb151c'
        }]
      });
    } else {
      totalCanvas.updateData({
        series: [{
          name: 'monthPay',
          data: 1,
          color: '#ff979b'
        }, {
          name: 'tLoan',
          data: 1,
          color: '#f9545b',
        }, {
          name: 'loan',
          data: 1,
          color: '#bb151c'
        }]
      });
    }
  },
  current2: function (price) {
    if (wx.openBluetoothAdapter) {
      wx.openBluetoothAdapter();
      var cImp = wx.createContext();
      this.textCharts(cImp, 'cInterest-month-pay', this.data.cInterestMonthPay);
    } else {
      this.setData({
        version: 'old'
      })
    }
    interestCanvas.updateData({
      series: [{
        name: 'down',
        data: JSON.parse(this.data.down),
        color: '#ff979b',
      }, {
        name: 'loan',
        data: JSON.parse(this.data.loan),
        color: '#f9545b'
      }, {
        name: 'interest',
        data: JSON.parse(this.data.cInterest),
        color: '#bb151c'
      }]
    });

  },
  textCharts(name, id, price) {
    var res = wx.getSystemInfoSync();
    var canvasWidth = res.windowWidth * 0.2 / 2 - 8;
    var smallc = (res.windowWidth * 0.2 + 50) / 2;

    name.beginPath();
    name.setStrokeStyle('white')
    name.setFillStyle('white');
    name.arc(smallc, smallc, canvasWidth, 0, 2 * Math.PI);
    name.stroke();
    name.fill();

    name.setTextAlign('center');
    name.setFillStyle('#323232');
    name.setFontSize(10);
    name.fillText("月供", smallc, smallc * 0.9);
    name.setFontSize(11);
    name.fillText("$" + price, smallc, smallc * 1.15);

    wx.drawCanvas({
      canvasId: id,
      actions: name.getActions()
    })
  },
  onLoad: function (options) {
    var that = this
    
    //调用应用实例的方法获取全局数据
    /*app.getUserInfo(function (data) {
      //更新数据
      that.setData({
        userInfo: data.userInfo,
        openid: data.openid,
      })
    })*/

    var date = new Date();
    var today = (date.getMonth() + 1) + '月' + date.getDate() + '日';
    var that = this;

    wx.request({
      url: config.http + '/api/config/info', //抓取每日變動的數據 hibor
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: { name: "hibor_daily_rate" },
      success: function (res) {
        //console.log(res)
        if (res.data.status == 1) {
          that.setData({
            monthInfo: parseFloat((res.data.data.info)),
            today: today
          })
        }
      },
      complete: function () {
        wx.request({
          url: config.http + '/api/config/info', //獲得hibor固定利率
          header: { 'content-type': 'application/x-www-form-urlencoded' },
          data: { name: 'hibor_fixed_rate' },
          success: function (res) {
            if (res.data.status == 1) {
              that.setData({
                fixed: parseFloat(res.data.data.info),
              })
            }
          },
          complete: function () {
            that.setData({
              interestFixed: parseFloat((that.data.monthInfo + that.data.fixed).toFixed(2)),
              yearInterest: (that.data.monthInfo + that.data.fixed).toFixed(2) * 100
            })
            //console.log((that.data.monthInfo+that.data.fixed).toFixed(2)*100)
          }
        })
        wx.request({
          url: config.http + '/api/config/info',
          header: { 'content-type': 'application/x-www-form-urlencoded' },
          data: {
            name: 'pressure_rate'
          },
          success: function (res) {
            //console.log(res.data)
            that.setData({
              pressure_rate: parseFloat(res.data.data.info)
            })
          },
        })
      }
    })


    var res = wx.getSystemInfoSync();
    this.setData({ windowHeight: res.windowHeight + 'px' })
    var canvasWidth = res.windowWidth * 0.2 + 50;

    if (wx.openBluetoothAdapter) {
      wx.openBluetoothAdapter();

      var imp = wx.createContext();
      this.textCharts(imp, 'interest-month-pay', this.data.allPay);
      var cImp = wx.createContext();
      this.textCharts(cImp, 'cInterest-month-pay', this.data.cInterestMonthPay);
      var total = wx.createContext();
      this.textCharts(total, 'total-month-repayment', this.data.totalMonthPay);
    } else {
      this.setData({
        version: 'old'
      })
    }

    mmpCanvas = new wxCharts({
      animation: false,
      canvasId: 'mmp-Canvas',
      type: 'pie',
      legend: false,
      disablePieStroke: true,
      dataLabel: false,

      series: [{
        name: 'down',
        data: 1,
        color: '#ff979b',

      }, {
        name: 'loan',
        data: 1,
        color: '#f9545b'
      }, {
        name: 'interest',
        data: 1,
        color: '#bb151c'
      }],
      width: canvasWidth,
      height: canvasWidth
    });

    interestCanvas = new wxCharts({
      animation: false,
      canvasId: 'interest-Canvas',
      type: 'pie',
      legend: false,
      disablePieStroke: true,
      dataLabel: false,
      series: [{
        name: 'down',
        data: 1,
        color: '#ff979b'
      }, {
        name: 'loan',
        data: 1,
        color: '#f9545b'
      }, {
        name: 'interest',
        data: 1,
        color: '#bb151c'
      }],
      width: canvasWidth,
      height: canvasWidth
    });
    totalCanvas = new wxCharts({
      animation: false,
      canvasId: 'total-repayment',
      type: 'pie',
      legend: false,
      disablePieStroke: true,
      dataLabel: false,

      series: [{
        name: 'monthPay',
        data: 1,
        color: '#ff979b'
      }, {
        name: 'tLoan',
        data: 1,
        color: '#f9545b'
      }, {
        name: 'total',
        data: 1,
        color: '#bb151c'
      }],
      width: canvasWidth,
      height: canvasWidth
    });

    
  },
  onReady: function (e) {




  }
})
